package com.electric.qrapp.ui.base.act

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.ViewGroup
import android.view.Window
import androidx.annotation.ColorRes
import androidx.annotation.LayoutRes
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleObserver
import com.electric.qrapp.R
import com.electric.qrapp.utils.function.addStatusBarHeightPadding
import com.electric.qrapp.utils.function.findColors
import com.electric.qrapp.utils.system.StatusBarUtil
import com.electric.qrapp.utils.view.LoadingUtils


/**
 *@author created by WA03675
 *@date  2021-08-02 13:39
 *@des
 */
abstract class SimpleActivity : AppCompatActivity() {


    private val loadingUtils: LoadingUtils by lazy {
        LoadingUtils()
    }

    protected val mActivity: AppCompatActivity
        get() = this

    override fun onCreate(savedInstanceState: Bundle?) {
        beforeCreate()
        super.onCreate(savedInstanceState)
        if (!useDataBinding()) {
            setContentView(getLayoutId())
        }

        setConfig()
        init()
    }

    private fun setConfig() {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    private fun init() {
        setBarColor()
        setAnim()
        initFirst()
        initView()
        initData()
    }


    private fun setAnim() {
//        overridePendingTransition(R.anim.nav_default_enter_anim,R.anim.nav_default_exit_anim)
    }


    @LayoutRes
    abstract fun getLayoutId(): Int

    /*
     *不使用DataBinding的情况
     */
    protected open fun useDataBinding() = false

    protected open fun initFirst() {}


    abstract fun initView()

    /*
     *初始化数据
     */
    abstract fun initData()


    protected open fun beforeCreate() {
        if (actAnim()) {
            window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        }
    }


    @MainThread
    fun addObserver(observer: LifecycleObserver) {
        lifecycle.addObserver(observer)
    }

    /*
     *过渡动画
     * @return false 为不设置
     * 如果设置了过渡动画请返回true
     */
    protected open fun actAnim(): Boolean {
        return false
    }

    /*
     * TODO
     * @param colorId 状态栏颜色
     * @param dark  状态栏文字颜色
     * @param transparencyBar 状态栏是否透明
     */
    @Suppress
    open fun setBarColor(
        @ColorRes colorId: Int = R.color.white,
        dark: Boolean = true,
        transparencyBar: Boolean = false,
        isAutoHeight: Boolean = true,
        titleView: ViewGroup? = null
    ) {
        if (transparencyBar) {
            StatusBarUtil.translucentStatusBar(mActivity, true)
        } else {
            StatusBarUtil.setStatusBarColor(mActivity, findColors(mActivity, colorId))
        }
        if (dark) {
            StatusBarUtil.changeToLightStatusBar(this)
        } else {
            StatusBarUtil.cancelLightStatusBar(this)
        }
        if (isAutoHeight && titleView != null) {
            addStatusBarHeightPadding(titleView)
        }
    }

    protected fun showLoad(isShow: Boolean, msg: String? = null) {
        loadingUtils.showLoading(this, isShow, msg)
    }

}